package gov.pbc.nn.kjgl.software.view;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.variant.Record;
import com.bstek.uflo.client.service.ProcessClient;
import com.bstek.uflo.client.service.TaskClient;
import com.bstek.uflo.env.Context;
import com.bstek.uflo.model.ProcessInstance;
import com.bstek.uflo.model.task.Task;
import com.bstek.uflo.process.node.TaskNode;
import com.bstek.uflo.service.TaskOpinion;

import gov.pbc.nn.core.bdf.entity.Dept;
import gov.pbc.nn.core.dao.PbcnnCoreHibernateDao;
import gov.pbc.nn.core.utils.SecurityUtils;
import gov.pbc.nn.kjgl.Constants;
import gov.pbc.nn.kjgl.entity.Project;
import gov.pbc.nn.kjgl.software.dao.KjkShDao;
import gov.pbc.nn.uflo.entity.Opinion;

@Component
public class JcywSHPR {

	@Autowired
	@Qualifier(ProcessClient.BEAN_ID)
	private ProcessClient processClient;

	@Autowired
	@Qualifier(TaskClient.BEAN_ID)
	private TaskClient taskClient;

	@Resource(name = PbcnnCoreHibernateDao.BEAN_ID)
	private PbcnnCoreHibernateDao coreHibernateDao;

	@Autowired
	private KjkShDao kjkShDao;

	/**
	 * 不通过 直接结束
	 * 
	 * @param taskId
	 * @param opinion
	 */
	@Expose
	@Transactional
	public void ajaxJcywNo(String taskId, String opinion, Project project) {
		TaskOpinion option = new TaskOpinion(opinion);
		Task task = taskClient.getTask(Long.valueOf(taskId));
		// Map<String, Object> map = new HashMap<String, Object>();
		taskClient.start(Long.valueOf(taskId));
		// map.put("agree", 1);
		final Opinion o = new Opinion();
		o.setBusKey(task.getBusinessId());
		o.setTaskId(Long.parseLong(taskId));
		o.setNodeName(task.getNodeName());
		// 操作那张表呢
		o.setBusTable("T_PROJECT");
		o.setOpinion(opinion);
		o.setTaskName(task.getTaskName());
		o.setProcessName("立项申请");
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		this.coreHibernateDao.save(o);
		// 完成流程
		project.setState(15);
		this.kjkShDao.update(project);
		taskClient.complete(Long.valueOf(taskId), "to 结束4", option);
	}

	/**
	 * 通过 如果czUserName为 null 直接结束 否则就需要处领导审核
	 * 
	 * @param taskId
	 * @param opinion
	 */
	@Expose
	@Transactional
	public void ajaxJcywOK(String taskId, String opinion, String czUserName, Project project) {
		TaskOpinion option = new TaskOpinion(opinion);
		Task task = taskClient.getTask(Long.valueOf(taskId));
		// Map<String, Object> map = new HashMap<String, Object>();
		taskClient.start(Long.valueOf(taskId));
		// map.put("agree", 1);
		final Opinion o = new Opinion();
		o.setBusKey(task.getBusinessId());
		o.setTaskId(Long.parseLong(taskId));
		o.setNodeName(task.getNodeName());
		// 操作那张表呢
		o.setBusTable("T_PROJECT");
		o.setOpinion(opinion);
		o.setTaskName(task.getTaskName());
		o.setProcessName("立项申请");
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		this.coreHibernateDao.save(o);
		// 完成流程
		if (czUserName == null) {
			// 如果czUserName 为null，则说明
			// 自己可以处理，并且已经通过，不需要处领导审核，否则就是自己审核通过，但是需要出领导审核
			project.setState(10);
			this.kjkShDao.update(project);
			taskClient.complete(Long.valueOf(taskId), "to 结束4", option);
		} else {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("czUserName", czUserName);
			String dm = null;
			if (project.getDeclareMeans().equals("1")) {
				dm = "集中式立项申请";
			} else {
				dm = "非集中式立项申请";
			}
			map.put("taskname", dm + "【" + project.getName() + "】" + "请处领导审核");
			taskClient.complete(Long.valueOf(taskId), "to 处领导审核", map, option);
		}
	}

	/**
	 * 驳回 退回给申请人
	 * 
	 * @param taskId
	 * @param opinion
	 */
	@Expose
	@Transactional
	public void ajaxJcywBH(String taskId, String opinion, int hldqz, Project project) {
		TaskOpinion option = new TaskOpinion(opinion);
		Task task = taskClient.getTask(Long.valueOf(taskId));
		taskClient.start(Long.valueOf(taskId));
		final Opinion o = new Opinion();
		o.setBusKey(task.getBusinessId());
		o.setTaskId(Long.parseLong(taskId));
		o.setNodeName(task.getNodeName());
		// 操作那张表呢
		o.setBusTable("T_PROJECT");
		o.setOpinion(opinion);
		o.setTaskName(task.getTaskName());
		o.setProcessName("立项申请");
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		this.coreHibernateDao.save(o);
		// 完成流程
		Map<String, Object> map = new HashMap<String, Object>();
		String dm = null;
		if (project.getDeclareMeans().equals("1")) {
			dm = "集中式立项申请";
		} else {
			dm = "非集中式立项申请";
		}
		if (hldqz == 1) {
			project.setHldqz(1);
			this.kjkShDao.update(project);
			map.put("taskname", dm + "【" + project.getName() + "】" + "需要行领导签字");
			taskClient.complete(Long.valueOf(taskId), "to 开始4", map, option);
		} else {
			project.setHldqz(0);
			this.kjkShDao.update(project);
			map.put("taskname", dm + "【" + project.getName() + "】" + "需要进行调整");
			taskClient.complete(Long.valueOf(taskId), "to 开始4",map, option);
		}
	}

	/**
	 * 其他部门处理
	 * 
	 * @param taskId
	 * @param deptId
	 */
	@Expose
	@Transactional
	public void ajaxJcywOther(String taskId, String deptId, Project project) {
		taskClient.start(Long.valueOf(taskId));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptId", deptId);
		String dm = null;
		if (project.getDeclareMeans().equals("1")) {
			dm = "集中式立项申请";
		} else {
			dm = "非集中式立项申请";
		}
		map.put("taskname", dm + "【" + project.getName() + "】" + "需要贵单位审核");
		taskClient.complete(Long.valueOf(taskId), "to 其他科", map);
	}

	@Expose
	@Transactional
	public String ajaxIsExist(Record dept) {
		Dept loginDept = (Dept) ContextHolder.getLoginUser().getDepts().get(0);
		Dept loginZZDept = (Dept) SecurityUtils.getZZByDept(loginDept);
		if (StringUtils.isNotBlank(dept.getString("deptLevel"))) {
			return "【" + dept.getString("name") + "】不属于部门,请重新选择!";
		} else if (dept.getString("id").startsWith(loginZZDept.getId()) == false) {
			return "【" + dept.getString("name") + "】不属于您所在的中支,请重新选择!";
		} else {
			List<IUser> kzUsers = SecurityUtils.getUserByRoleForDept(dept.getString("id"), Constants.ROLE_LXSHY);
			if (kzUsers.size() <= 0) {
				return "【" + dept.getString("name") + "】未设置【" + Constants.ROLE_LXSHY + "】角色,无法处理该任务！";
			}
		}
		return null;
	}

}
